package cn.usian.controller;

import cn.usian.client.UserClient;
import cn.usian.pojo.Order;
import cn.usian.pojo.User;
import cn.usian.service.OrderService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserClient userClient;
    @GetMapping("{id}")
    public Order findById(@PathVariable Long id){
        Order order= orderService.findById(id);
       //声明请求的url
        /*String url="http://localhost:8082/user/"+order.getUserId();*/
        //String url="http://userservice/user/"+order.getUserId();
       //远程调用方法
        User user = userClient.findById(order.getUserId());
       //将user对象封装到order中
        order.setUser(user);
        return order;
    }
    @GetMapping("/findAll")
    public List<User>findAll(){
        return userClient.findAll();
    }
    @SentinelResource("hot")
    @GetMapping("{orderId")
    public Order querOrderByUserId(@PathVariable("orderId")Long orderId){
        //根据id查询定点并返回
        return orderService.queryOrderById(orderId);
    }
}
